package org.example.reservas_mesas;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.google.android.gcm.GCMRegistrar;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import java.util.List;
import org.example.library.Invoker;
import org.example.library.RegistrarUsuario;
import org.example.library.Restaurantes;
import org.example.library.ServidorPHP;


public class usuario extends MapActivity {
    String user;
    TextView txt_usr, logoff;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_screen);
        txt_usr= (TextView) findViewById(R.id.usr_name);
        logoff= (TextView) findViewById(R.id.logoff);          
        Bundle extras = getIntent().getExtras();

        if (extras != null) user  = extras.getString("usuario");
        else user="error";

        SharedPreferences prefs = getSharedPreferences("MisPreferencias", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        
        editor.putString("usuario", user);
        editor.commit();
        this.unregisterUser(this.getApplicationContext());//esto es por uso de emulador
        this.registerUser(this.getApplicationContext());

        
        txt_usr.setText(user);
        
        logoff.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Intent i=new Intent(usuario.this,principal.class);
                startActivity(i);
            }
        });
        
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        List<Overlay> mapOverlays = mapView.getOverlays();
        Restaurantes rest = new Restaurantes(usuario.this,detalle.class,user);
        rest.AddRestaurantes(this,mapOverlays);
    }
	 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) return true;
        return super.onKeyDown(keyCode, event);
    }
    
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.MnuOpc1:
                Intent i=new Intent(usuario.this,listado.class);
                i.putExtra("usuario", user);
                startActivity(i);
                finish();
                return true;
            case R.id.MnuOpc2:
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                return true;
            default:
                return false;
        }
    }
    
    private void registerUser(Context context){
        final String regId = GCMRegistrar.getRegistrationId(context);

        if (regId.equals("")) {
         GCMRegistrar.register(context, "467373484049"); 
        }else {           
            Invoker invoker = new Invoker();
            ServidorPHP servidor = new ServidorPHP();
            servidor.SetMailComensal(user);
            servidor.SetIDReg(regId);
            RegistrarUsuario RU = new RegistrarUsuario(servidor);
            invoker.SetPeticion(RU);
            invoker.Ejecuta();
        } 
    }
    
    private void unregisterUser(Context context){
    	GCMRegistrar.checkDevice(this);
    	GCMRegistrar.checkManifest(this);
     	 final String regId = GCMRegistrar.getRegistrationId(context);
     	        if (!regId.equals("")) {
     	            GCMRegistrar.unregister(this);
     	        } else {}
     	        
     	      
    }
}
